package com.stylefeng.guns.rest.modular.cinema;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.cinema.CinemaServiceAPI;
import com.stylefeng.guns.api.cinema.vo.*;
import com.stylefeng.guns.core.response.ServerResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/cinema/")
public class CinemaController {

    @Reference(interfaceClass = CinemaServiceAPI.class,
            connections = 10, cache = "lru", check = false)
    private CinemaServiceAPI cinemaServiceAPI;

    private static final String IMG_PRE = "http://img.meetingshop.cn/";

    @RequestMapping(value = "getCinemas")
    public ServerResponse getCinemas(CinemaQueryVo cinemaQueryVo) {
        try {
            // 按照五个条件进行筛选
            Page<CinemaVo> cinemas = cinemaServiceAPI.getCinemas(cinemaQueryVo);
            // 判断是否有满足条件的影院
            if (cinemas.getRecords() == null || cinemas.getRecords().size() == 0) {
                return ServerResponse.createBySuccess("没有影院可查");
            } else {
                return ServerResponse.createBySuccess(cinemas);
            }

        } catch (Exception e) {
            // 如果出现异常，应该如何处理
            log.error("获取影院列表异常", e);
            return ServerResponse.createByError("查询影院列表失败");
        }
    }

    // 获取影院的查询条件

    /**
     * 特点
     *         1、热点数据 -> 放缓存
     *         2、banner
     * @param cinemaQueryVo     查询信息
     * @return
     */
    @RequestMapping(value = "getCondition")
    public ServerResponse getCondition(CinemaQueryVo cinemaQueryVo) {
        try {
            // 获取三个集合，然后封装成一个对象返回即可
            List<BrandVo> brands = cinemaServiceAPI.getBrands(cinemaQueryVo.getBrandId());
            List<AreaVo> areas = cinemaServiceAPI.getAreas(cinemaQueryVo.getDistrictId());
            List<HallTypeVo> hallTypes = cinemaServiceAPI.getHallTypes(cinemaQueryVo.getHallType());

            Map<String, List> resMap = new HashMap<>(3);
            resMap.put("areaList", areas);
            resMap.put("brandList", brands);
            resMap.put("halltypeList", hallTypes);

            return ServerResponse.createBySuccess(resMap);
        } catch (Exception e) {
            log.error("获取条件列表失败", e);
            return ServerResponse.createByError("获取影院查询条件失败");
        }
    }


    @RequestMapping(value = "getFields")
    public ServerResponse getFields(Integer cinemaId) {
        try {

            CinemaInfoVo cinemaInfoById = cinemaServiceAPI.getCinemaInfoById(cinemaId);

            List<FilmInfoVo> filmInfoByCinemaId = cinemaServiceAPI.getFilmInfoByCinemaId(cinemaId);

            Map<String, Object> resMap = new HashMap<>(2);
            resMap.put("cinemaInfo", cinemaInfoById);
            resMap.put("filmList", filmInfoByCinemaId);

            return ServerResponse.createBySuccess(IMG_PRE, resMap);
        } catch (Exception e) {
            log.error("获取播放场次失败", e);
            return ServerResponse.createByError("获取播放场次失败");
        }
    }

    @RequestMapping(value = "getFieldInfo", method = RequestMethod.POST)
    public ServerResponse getFieldInfo(Integer cinemaId, Integer fieldId) {
        try {

            CinemaInfoVo cinemaInfoById = cinemaServiceAPI.getCinemaInfoById(cinemaId);
            FilmInfoVo filmInfoByFieldId = cinemaServiceAPI.getFilmInfoByFieldId(fieldId);
            HallInfoVo filmFieldInfo = cinemaServiceAPI.getFilmFieldInfo(fieldId);

            // 造几个销售的假数据，后续会对接订单接口
            filmFieldInfo.setSoldSeats("1,2,3");

            Map<String, Object> resMap = new HashMap<>(3);
            resMap.put("cinemaInfo", cinemaInfoById);
            resMap.put("filmList", filmInfoByFieldId);
            resMap.put("hallInfo", filmFieldInfo);

            return ServerResponse.createBySuccess(IMG_PRE, resMap);
        } catch (Exception e) {
            log.error("获取选座信息失败", e);
            return ServerResponse.createByError("获取选座信息失败");
        }
    }

}
